package stack;

public class ArrayStackDemo {

  public static void main(String[] args) {
    ArrayStack arrayStack = new ArrayStack(4);
    arrayStack.push(10);
    arrayStack.push(20);
    arrayStack.push(30);
    arrayStack.push(40);
    arrayStack.list();
    arrayStack.pop();
    arrayStack.list();
  }
}

class ArrayStack {
  int maxSize;
  int top = -1;
  int[] array;

  public ArrayStack(int maxSize) {
    this.maxSize = maxSize;
    this.array = new int[maxSize];
  }

  // 栈空
  public boolean isEmpty() {
    return top == -1;
  }
  // 栈满
  public boolean isFull() {
    return top == maxSize - 1;
  }
  // 入栈
  public void push(int value) {
    if (isFull()) {
      System.out.println("栈满");
      return;
    }
    top++;
    array[top] = value;
  }

  //出栈

  public void pop() {
    if (isEmpty()){
      System.out.println("栈空");
      return;
    }
    System.out.printf("array[%d]=%d出栈",top,array[top]);
    top--;
  }

  //遍历
  public void list(){
    if (isEmpty()){
      System.out.println("栈空");
    }
    for (int i=maxSize-1;i>=0;i--){
      System.out.println(array[i]);
    }
  }
}
